package com.wang.tool.okhttp;

import okhttp3.Request;

/**
 * @Description SSE标准响应生成器
 * @Author shaopeng
 * @Date 2023/5/10
 * @Version 1.0
 */
public interface SseGenerator<T> {

    /**
     * 示例
     * 填充req
     * json body
     * RequestBody reqbody = RequestBody.create(JSON.toJSONString(body), MediaType.parse("application/json; charset=utf-8"));
     * builder.post(reqbody);
     *
     * param body
     * FormBody.Builder formBodyBuilder = new FormBody.Builder();
     * formBodyBuilder.add(k,v);
     * builder.post(formBodyBuilder.build());
     *
     * 添加请求头
     * builder.addHeader("Authorization", "111");
     *
     * @param builder 非空
     * 抛出异常会转发到onError
     */
    void fillRequest(Request.Builder builder);

    /**
     * 流式吐出的单行结果
     * @param line 当前行  非空
     * 抛出异常会转发到onError
     */
    T onReadLine(String line);

    /**
     * 读取正常完成；调用结束
     * @return
     * 抛出异常会转发到onError
     */
    T onFinished();


    /**
     * 异常时
     * @param error 非空
     * @return 此函数返回则future不会感知异常
     * @throws Throwable 抛出异常会反映到结果的future中
     */
    T onError(Throwable error) throws Throwable;

}
